/*
 * 功能：公告控制器，懂得都懂
 * 作者：张杨(Sunny)
 * 日期： 2024/11/11 上午11:28
 */
package com.example.springboot.control;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Notice;
import com.example.springboot.entity.User;
import com.example.springboot.service.NoticeService;
import com.example.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 公告控制器，提供公告相关的CRUD（创建读取更新删除）操作
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    NoticeService noticeService;

    @Resource
    UserService userService;

    // 新增公告
    @PostMapping("/create")
    public Result create(@RequestBody Notice notice) {
        return noticeService.addNotice(notice);
    }

    // 修改公告
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return Result.success();
    }

    // 删除公告
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        noticeService.removeById(id);
        return Result.success();
    }

    // 批量删除公告
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        noticeService.removeBatchByIds(ids);
        return Result.success();
    }

    // 查询公开公告
    @GetMapping("/selectOpen")
    public Result selectOpen() {
        List<Notice> noticeList = noticeService.getOpenNotices();
        return Result.success(noticeList);
    }

    // 分页查询所有公告
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String title, // 标题
                               @RequestParam String username) { // 发布者用户名
        // 调用服务类的方法获取分页通知列表
        Page<Notice> page = noticeService.getNoticesByPage(pageNum, pageSize, title, username);
        // 处理结果，将用户名替换为用户对象
        for (Notice record : page.getRecords()) {
            String authorId = record.getUsername();
            User user = userService.getById(authorId);
            if (user != null) {
                record.setUser(user.getUsername());
            }
        }
        return Result.success(page);
    }

    //导出信息到本地 Excel 文件
    @AuthAccess
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String content,
                           @RequestParam(required = false) String createTime,
                           @RequestParam(required = false) String ids,
                           HttpServletResponse response) throws IOException {
        noticeService.exportData(content, createTime, ids, response);
    }

    // 导入信息到数据库
    @AuthAccess
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) {
        try {
            return noticeService.importData(file);
        } catch (IOException e) {
            return Result.error("导入失败");
        }
    }
}
